package com.ytjj.qmyx.admin.service.bigdata;

import com.ytjj.common.service.RedisService;
import com.ytjj.qmyx.admin.eunm.DynamicExposureTypeEnums;
import com.ytjj.qmyx.admin.utils.DateUtils;
import com.ytjj.qmyx.mall.mapper.DynamicExposureRecordMapper;
import com.ytjj.qmyx.mall.mapper.DynamicMapper;
import com.ytjj.qmyx.mall.model.DynamicExposureRecord;
import com.ytjj.qmyx.users.model.bigdata.AdsExpose;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Slf4j
@RefreshScope
public class AdsExposeService {
    @Value("${talent_uplineTime:20210727}")
    private String talentUplineTime;
    @Value("${talent_expose_base:30000}")
    private String exposeBaseStr;
    @Resource
    private DynamicExposureRecordMapper dynamicExposureRecordMapper;
    @Autowired
    private RedisService redisService;
    @Resource
    private DynamicMapper dynamicMapper;

    //曝光量扶持分配方案
    private static int[] exposeBaseSupport_1 = {7, 8, 9};
    private static int[] exposeBaseSupport_2 = {10, 11, 12};
    private static int[] exposeBaseSupport_3 = {20, 21, 22, 23};
    private static int[] exposeBaseSupport_4 = {21, 22, 23, 24};
    private static int[] exposeBaseSupport_5 = {15, 16, 17};
    private static int[] exposeBaseSupport_6 = {12, 13, 14, 15};
    private static int[] exposeBaseSupport_7 = {10, 11, 12, 13};
    private static int[] exposeBaseSupport_8 = {5, 6, 7, 8};

    public void scheduleTask(List<AdsExpose> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        log.debug("===scheduleTask===");
        Random random = new Random();
        int[] maps = {3, 4, 5};
        list.stream().forEach(item -> {
            //获取该用户最后测评通过的时间
            int gapDay = dynamicTime(Integer.valueOf(item.getUserid()));
            DynamicExposureRecord record = new DynamicExposureRecord();
            record.setUserId(Integer.parseInt(item.getUserid()));
            record.setCreateTime(new Date());
            int virtualExposure = 0;
            Integer exposeBase = Integer.parseInt(exposeBaseStr);

            switch (gapDay) {
                case 1:
                    virtualExposure = exposeBaseSupport_1[new Random().nextInt(exposeBaseSupport_1.length)];
                    break;
                case 2:
                    virtualExposure = exposeBaseSupport_2[new Random().nextInt(exposeBaseSupport_2.length)];
                    break;
                case 3:
                    virtualExposure = exposeBaseSupport_3[new Random().nextInt(exposeBaseSupport_3.length)];
                    break;
                case 4:
                    virtualExposure = exposeBaseSupport_4[new Random().nextInt(exposeBaseSupport_4.length)];
                    break;
                case 5:
                    virtualExposure = exposeBaseSupport_5[new Random().nextInt(exposeBaseSupport_5.length)];
                    break;
                case 6:
                    virtualExposure = exposeBaseSupport_6[new Random().nextInt(exposeBaseSupport_6.length)];
                    break;
                case 7:
                    virtualExposure = exposeBaseSupport_7[new Random().nextInt(exposeBaseSupport_7.length)];
                    break;
                case 8:
                    virtualExposure = exposeBaseSupport_8[new Random().nextInt(exposeBaseSupport_8.length)];
                    break;
                default:
                    virtualExposure = exposeBaseSupport_8[new Random().nextInt(exposeBaseSupport_8.length)];
                    break;
            }
            BigDecimal d_value = new BigDecimal(random.nextDouble()).setScale(2, BigDecimal.ROUND_HALF_UP);
            double multiple_v = new BigDecimal(virtualExposure).add(d_value).doubleValue();
            virtualExposure = (int) (exposeBase * multiple_v);

            record.setType(DynamicExposureTypeEnums.EXPOSURE.getCode().byteValue());
            if (0 >= virtualExposure) {
                record.setTaskValue(item.getExposeDayCount());
                record.setSupportValue(0);
            }else {
                record.setTaskValue(item.getExposeDayCount());//真实曝光量
                record.setSupportValue(virtualExposure / 100);//扶持数据
            }
            dynamicExposureRecordMapper.insertSelective(record);//新增曝光量记录
            //新增浏览量记录
            DynamicExposureRecord browse_record = new DynamicExposureRecord();
            browse_record.setUserId(Integer.parseInt(item.getUserid()));
            browse_record.setCreateTime(new Date());
            browse_record.setType(DynamicExposureTypeEnums.BROWSE.getCode().byteValue());
            int loadFactor = maps[random.nextInt(maps.length)];
            browse_record.setSupportValue(record.getSupportValue() * loadFactor / 100);//扶持浏览量
            browse_record.setTaskValue(item.getViewDayCount());//真实浏览量
            dynamicExposureRecordMapper.insertSelective(browse_record);
        });
    }

    /**
     * 获取上线到当前时间的间隔天数
     * @return
     */
    private int uplineGapDay() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        try {
            Date uplineTime = sdf.parse(talentUplineTime);
            Date curDate = new Date();
            int gapDay = DateUtils.differentDaysByMillisecond(uplineTime, curDate);
            return gapDay;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 获取上线到当前时间的间隔天数
     * @return
     */
    private int dynamicTime(Integer userId) {
        int gapDay = 0;
        try {
            //查询最后测评通过时间
            Date dynamicTime = dynamicMapper.selectTimeByUserId(userId);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            Date curDate = new Date();
            gapDay = DateUtils.differentDaysByMillisecond(dynamicTime, curDate);
            if (gapDay <= 0){
                return 0;
            }
        } catch (Exception e) {
            return gapDay;
        }
        return gapDay;
    }

}
